import { container, singleton } from "tsyringe";
import { RobotAppPage } from "../../page";
import { Context } from "../../context";
import { $ } from "../../../utils";

@singleton()
export class KwaiRnActivity extends RobotAppPage {
    title: string = `我的钱包`
    private isRuning: boolean = false;
    private ctx: Context;
    constructor() {
        super()
        this.ctx = container.resolve(Context)
    }
    dispose(): void {
        this.out()
    }
    packageName: string;
    is(packageName: string): boolean {
        this.packageName = packageName;
        return `com.smile.gifmaker` === packageName || `com.kuaishou.nebula` === packageName;
    }
    accept(activityName: string): boolean {
        return `com.kwai.kds.krn.api.page.KwaiRnActivity` === activityName;
    }
    in(pkg?: string): boolean {
        if (this.isRuning) return false;
        this.isRuning = true;
        const closeable = selector().className('ViewGroup').depth(7).drawingOrder(2).clickable(true).findOne()
        if (closeable) {
            $.clickButton(closeable)
            sleep(1000)
        }
        const textNodes = textMatches(/.{1,6}/).visibleToUser(true).find()
        if (textNodes.length > 0) {
            const texts = textNodes.map(node => node.text()).join(' ')
            const res = texts.match(/快币\(充值\)\s(\d+)/)
            if (res && res.length > 0) {
                const [, money] = res;
                console.log(`${pkg}-${money}`)
                if (pkg === `com.kuaishou.nebula`) {
                    this.ctx.updateUi({ nebulaCurrentMoney: money })
                } else {
                    this.ctx.updateUi({ kwaiCurrentMoney: money })
                }
            }
        }
        log(`KwaiRnActivity in`)
        return true;
    }
    out(): boolean {
        if (!this.isRuning) return false;
        this.isRuning = false;
        log(`KwaiRnActivity out`)
        return true;
    }
}